android client not working [migrated]

Posted by Syeda Zunairah on Programmers See other posts from Programmers or by Syeda Zunairah
Published on 2013-10-18T14:07:00Z Indexed on 2013/10/18 16:11 UTC
Read the original article Hit count: 156

Filed under:
|
|

i have a java client and c# server the server code is

static Socket listeningSocket;
        static Socket socket;
        static Thread thrReadRequest;
        static int iPort = 4444;
        static int iConnectionQueue = 100;
        static void Main(string[] args)
        {
            Console.WriteLine(IPAddress.Parse(getLocalIPAddress()).ToString());
            try
            {
                listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //listeningSocket.Bind(new IPEndPoint(0, iPort));                                
                listeningSocket.Bind(new IPEndPoint(IPAddress.Parse(getLocalIPAddress()), iPort));
                listeningSocket.Listen(iConnectionQueue);
                thrReadRequest = new Thread(new ThreadStart(getRequest));
                thrReadRequest.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine("Winsock error: " + e.ToString());
                //throw;
            }
        }
        static private void getRequest()
        {
            int i = 0;
            while (true)
            {
                i++;
                Console.WriteLine("Outside Try i = {0}", i.ToString());
                try
                {
                    socket = listeningSocket.Accept();
                    // Receiving
                    //byte[] rcvLenBytes = new byte[4];
                    //socket.Receive(rcvLenBytes);
                    //int rcvLen = System.BitConverter.ToInt32(rcvLenBytes, 0);
                    //byte[] rcvBytes = new byte[rcvLen];
                    //socket.Receive(rcvBytes);
                    //String formattedBuffer = System.Text.Encoding.ASCII.GetString(rcvBytes);
                    byte[] buffer = new byte[socket.SendBufferSize];
                    int iBufferLength = socket.Receive(buffer, 0, buffer.Length, 0);
                    Console.WriteLine("Received {0}", iBufferLength);
                    Array.Resize(ref buffer, iBufferLength);
                    string formattedBuffer = Encoding.ASCII.GetString(buffer);
                    Console.WriteLine("Data received by Client: {0}", formattedBuffer);
                    if (formattedBuffer == "quit")
                    {
                        socket.Close();
                        listeningSocket.Close();
                        Environment.Exit(0);
                    }
                    Console.WriteLine("Inside Try i = {0}", i.ToString());
                    Thread.Sleep(500);
                }
                catch (Exception e)
                {
                    //socket.Close();
                    Console.WriteLine("Receiving error: " + e.ToString());
                    Console.ReadKey();
                    //throw;
                }
                finally
                {
                    socket.Close();
                    //listeningsocket.close();
                }
            }
        }
        static private string getLocalIPAddress()
        {
            IPHostEntry host;
            string localIP = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    localIP = ip.ToString();
                    break;
                }
            }
            return localIP;
        }
    }

and the jave android code is

private TCPClient mTcpClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText editText = (EditText) findViewById(R.id.edit_message);
    Button send = (Button)findViewById(R.id.sendbutton);        
    // connect to the server
    new connectTask().execute("");
    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String message = editText.getText().toString(); 
            //sends the message to the server
            if (mTcpClient != null) {
                mTcpClient.sendMessage(message);
            }
            editText.setText("");
        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public class connectTask extends AsyncTask<String,String,TCPClient> {
    @Override
    protected TCPClient doInBackground(String... message) {
        mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
            @Override
            public void messageReceived(String message) {
                publishProgress(message);
            }
        });
        mTcpClient.run();
        return null;
    }
    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values); 
    }
}

}

when i run the server it gives output of try i=1. can any one tell me what to do next

© Programmers or respective owner

Related posts about c#

Related posts about android